Repository navigation
Analytics: Track feature adoption with setting values and a config snapshot - #1370
gabrielcld2 wants to merge 8 commits into
Conversation
…fig snapshot `settings_saved` only reported which keys changed, and features left at their defaults were never reported at all, so adoption of lazy loading, responsive images, formats or adaptive streaming couldn't be measured. - `settings_saved` now carries `changed_values` for an allowlist of image, lazy loading, responsive and video settings. Anything outside the list, including the connection string, is never sent. - Values are normalised: toggles to booleans, numbers to ints, free-text transformations to a presence flag, and crop sizes to a count of custom crops. - A one-time `feature_config_snapshot` sends the full feature config on the first admin load once connected, once per cloud name. It covers wizard setups, constant-based connections, account switches and existing installs upgrading.
- Send the snapshot only on admin page loads by administrators, never from admin-ajax requests or for other roles. - Autoload the one-time flag so checking it costs no query, and gate the first send with add_option() so concurrent requests can't both send it. - Leave missing setting values out instead of reporting them as off. - Send the lazy-loading threshold only when it is a CSS length; it is a free-text field. - Allowlist feature settings per settings page. - Report `changed_values` as read back after the save, so they reflect any save filters. - Remove the analytics one-time flags on uninstall.
…e feature snapshot The feature_config_snapshot event now carries a trigger param: setup when the site was installed or seen unconnected by this version before being connected (or switched account), upgrade when it was already connected. Account switches also go through the same one-request-only gate. "Max images" is derived by the settings page from the breakpoint distance and the min/max widths, and only stored once that page is saved, so the snapshot now reports the count the page shows.
Every settings save now carries the whole allowlisted feature configuration as flat fields, the same as feature_config_snapshot, next to page and changed_keys. Each save is then a baseline, and changed_keys shows which settings were deliberately overridden. This replaces the nested changed_values object.
- Select and radio values are only sent when they are one of the setting's options, and as "other" otherwise, since they are only sanitised as text and filters can return anything. - The lazy-loading threshold is lowercased, so "100PX" is kept. - "Max images" is marked as derived in the allowlist, since the stored value is never sent. - settings_saved no longer builds the feature configuration while analytics are disabled. - The concurrency test for the first snapshot now really reaches the add_option() gate. - The snapshot docs now say it is re-sent on every account change, and where the setup/upgrade trigger can be wrong.
| $params = array_merge( | ||
| $analytics->get_feature_snapshot(), | ||
| array( | ||
| 'page' => $submission, | ||
| 'changed_keys' => $changed_keys, | ||
| ) | ||
| ); | ||
|
|
||
| $analytics->track( 'settings_saved', 'settings', null, $params ); |
There was a problem hiding this comment.
warning: This does not match the ticket contract for settings_saved. The ticket asks for the changed keys as key→value pairs (like sync_settings_changed). This sends the full feature snapshot (about 30 keys) on every save, also for pages that are not feature pages (connect, sync, gallery, extensions). Two results:
- On a save, the event does not show which value changed. The collector must compare
changed_keyswith the flat snapshot keys. For keys that are not in the allowlist, no value is sent. - Each save of a non-feature page sends the full feature config again. This adds noise to the
settings_savedadoption counts and makes the event payload bigger.
If this design is intentional, get approval from the data team and update the Event Tracking Spec and the collector schema for these extra params. If it is not, send changed_values (key → normalised value) for the changed keys that are in the allowlist, and send nothing more when $submission is not a key of Analytics::FEATURE_SETTINGS.
There was a problem hiding this comment.
Thanks! The full config is intentional: We decided in a sync after the ticket was written to send a flat map of all feature settings with each save, rather than only the changed values, so every save is a complete baseline. changed_keys still says what changed, and each changed allowlisted key has its new value at the top level. We'll update the Event Tracking Spec once the payloads are final.
You're right about non-feature pages, though: they can't change the feature config, so it was just noise. Fixed in 9051498: only saves of the image, video, lazy-loading and responsive pages carry it now, and the others send only page and changed_keys.
| $claimed = false === $sent | ||
| ? add_option( self::FEATURE_SNAPSHOT_FLAG, $cloud, '', true ) | ||
| : update_option( self::FEATURE_SNAPSHOT_FLAG, $cloud, true ); | ||
| if ( ! $claimed ) { | ||
| return; | ||
| } | ||
|
|
||
| $params = $this->get_feature_snapshot(); | ||
| $params['trigger'] = false === $sent ? 'upgrade' : 'setup'; |
There was a problem hiding this comment.
nit: In one race, trigger can be upgrade when it should be setup. Request A reads the flag as missing, so its notoptions cache marks the flag as missing. Then request B (still unconnected) writes ''. Then A, now connected, calls add_option(). add_option() does not check again because notoptions has the flag, so the INSERT ... ON DUPLICATE KEY UPDATE replaces '', returns true, and A sends trigger: upgrade. This needs a concurrent unconnected admin load at the moment of connection, so it is rare. Add this case to the list of known limits in the docblock at L212-223.
There was a problem hiding this comment.
Good catch, confirmed. It's rare and only mislabels the trigger (still a single snapshot), so I've added it to the known limits in the docblock (9051498).
| return; | ||
| } | ||
|
|
||
| // Autoloaded, so this check costs no query on later admin loads. |
There was a problem hiding this comment.
nit: This comment is not always correct. When the site is connected, the flag is missing, and cloudinary_analytics_enabled returns false, the method returns at L248 before it writes the flag. Then get_option() misses alloptions and runs a SELECT on each admin load (there is no persistent object cache). Change the comment, or write the flag before the is_enabled() check.
There was a problem hiding this comment.
👍 Thanks.
I kept the behaviour, because writing the flag there would lose both the deferred send and the upgrade trigger, and corrected the comment to say that case costs one option query per admin load (9051498).
| foreach ( $values as $key => $value ) { | ||
| $original[ $key ] = $settings->get_value( $key ); | ||
| $settings->get_setting( $key )->set_value( $value ); | ||
| } | ||
|
|
||
| $snapshot = $this->analytics->get_feature_snapshot(); | ||
|
|
||
| $settings->get_setting( 'pixel_step' )->set_value( 500 ); | ||
| $coarser = $this->analytics->get_breakpoint_count(); | ||
|
|
||
| foreach ( $original as $key => $value ) { | ||
| $settings->get_setting( $key )->set_value( $value ); | ||
| } |
There was a problem hiding this comment.
nit: This test changes the shared in-memory settings on the plugin singleton and restores them only on the success path. If get_feature_snapshot() or get_breakpoint_count() throws, the next tests run with max_width=2048, pixel_step=500 and so on. Put the restore loop in finally, or in tear_down().
There was a problem hiding this comment.
Done, the restore now runs in a finally block (9051498).
… saves Saves of the connection, sync, gallery or extensions pages can't change the feature configuration, so their settings_saved events no longer carry it. Also documents one more race where the snapshot trigger can be upgrade instead of setup, corrects the comment on the flag's query cost, and restores shared settings in a finally block in the breakpoints test.
Makes feature adoption measurable: which delivery features each install runs, and with which values.
The problem
settings_savedonly carrieschanged_keys, not the values.Approach
Feature configuration, shared by both events
Analytics::FEATURE_SETTINGS, keyed by settings page, lists the image, video, lazy loading and responsive image settings whose values may be sent. Everything else, including the connection string, is never sent.otherotherwise. Those fields are only sanitised as text, and filters can return anything, so this keeps stored free text from being sent.100px(lowercased).image_freeform,video_freeform) can contain public IDs, overlay text or URLs, so they are only sent as a presence boolean.crop_sizesis sent as the number of image sizes with a custom crop.breakpoints("Max images") is sent as the count the settings page shows, derived from the breakpoint distance and the min/max widths (2048 / 200 / 200 gives 10). The page calculates that field and only stores it once it is saved, and the stored value isn't used for delivery.settings_savedpageandchanged_keys, so each such save is a baseline andchanged_keysshows what was deliberately overridden.pageandchanged_keys.cloudinary_settings_save_settingfilters rather than what was submitted.{ "event_name": "settings_saved", "page": "image_settings", "changed_keys": ["image_quality"], "image_delivery": true, "image_format": "auto", "image_quality": "80", "use_lazy_load": true, "dpr": "2X", "enable_breakpoints": false, "pixel_step": 200, "breakpoints": 10, "video_delivery": true, "video_quality": "auto" }(Abridged: every allowlisted setting is included.)
One-time
feature_config_snapshotevent (categoryfeatures)triggerfield tells the cases apart:upgrade: the site was already connected the first time this version ran. This is the baseline backfill for existing installs.setup: the site was freshly installed, or seen unconnected by this version, before being connected, including through theCLOUDINARY_CONNECTION_STRINGconstant. Account switches also count assetup.setupon a fresh install. A reinstall after the uninstall clean-up (which keeps the plugin's DB version) and subsites of a network activation reportupgradeinstead.add_option()orupdate_option(), so concurrent requests can't both send it.cloudinary_analytics_enabled, so it is still sent if they are turned on later.first_api_consumptionone) are removed by the uninstall clean-up, so a reinstall sends a new snapshot.Configured vs in effect
The values are what is configured, not necessarily what is in effect:
image_format: "avif"withimage_delivery: false, orpixel_stepwithenable_breakpoints: false. The parent toggles are in the same payload, so reports should filter on them.cloudinary_setting_get_valueare reported as filtered, since they are read through the settings API.cloudinary_is_{feature}_active, which are meant for front-end requests) and per-image ones (cloudinary_lazy_load_bypass,cloudinary_skip_responsive_breakpoints,cloudinary_is_deliverable). These should be rare.QA notes
npm run test:unit -- --filter Test_Analytics_Featurecovers:setupvsupgrade, not sent while disconnected, with analytics off, for non-administrators or from admin-ajax, and only one of several concurrent requests sending it.tests/e2e/settings-analytics.spec.jschecks the values on a save of each feature page (image, video, lazy loading and responsive), and that the other feature pages' settings are included.tests/e2e/features-analytics.spec.jschecks that a gallery save doesn't carry the configuration.tests/e2e/features-analytics.spec.jschecks the snapshot's admin wiring. It is tagged@serialbecause its one-time flag is shared by the whole site.wp option delete _cloudinary_feature_snapshot_emitted.feature_config_snapshotevent, withtrigger: "upgrade"and the current feature values.trigger: "setup". (On a fresh install, the first snapshot after connecting is alsosetup.)settings_savedevent should list the changed keys and carry the full feature configuration with the new values.settings_savedevent should carry onlypageandchanged_keys, and no connection details should appear in any event.